package com.shop2cn.iapi.sqoptrade.testcase;

import java.math.BigDecimal;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Ignore;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.shop2cn.iapi.shenqisettlement.parameter.bean.AgentProductBean;
import com.shop2cn.iapi.sqoptrade.parameter.req.OnLogisticsNodeChangeReq;
import com.shop2cn.iapi.sqoptrade.parameter.vo.OnLogisticsNodeChangeBizParamVo;
import com.shop2cn.iapi.sqoptrade.resp.Order;
import com.shop2cn.iapi.sqoptrade.resp.TradingServiceResp;
import com.shop2cn.iapi.sqoptrade.service.OnLogisticsNodeChangeCall;
import com.shop2cn.iapi.sqsynctrade.service.TradingSyncService;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.TestCase;
import com.ymt.utils.annotation.TestSuite;
import com.ymt.utils.tag.P1;
import com.ymttest.business.service.SQPayGateWayCallService;
import com.ymttest.business.service.shenqi.DGSQBuildProxyChainCallService;
import com.ymttest.business.service.shenqi.SqTradingCallService;
import com.ymttest.business.service.shenqi.bean.ShenqiUserSet;
import com.ymttest.database.sharding.db.sharding.model.OrderPo;
import com.ymttest.database.sharding.db.sqlwapper.OrderShardingWapper;

@TestSuite
public class Ts_SqOnLogisticsNodeChange {

	private static OnLogisticsNodeChangeReq onLogisticsNodeChangeReq;
	private static OnLogisticsNodeChangeCall onLogisticsNodeChangeCall;

	private static OrderShardingWapper orderShardingWapper = new OrderShardingWapper();
	
	private static long cdfMembergoodsOwnerId = ShenqiUserSet.cdfMembergoodsOwnerId;
	
//	private static long cdfMembergoodsOwnerId = 500772144L;
	
	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("订单的运单全部签收，同步签收状态");
	}

	@Before
	public void caseUp() {
		onLogisticsNodeChangeReq = new OnLogisticsNodeChangeReq();
		onLogisticsNodeChangeCall = new OnLogisticsNodeChangeCall();
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(onLogisticsNodeChangeCall.getOpurl());
	}

	@After
	public void caseDown() {
		Logger.end();
	}

	@Test
	@Category(P1.class)
	@TestCase
	@Ignore
	// 需要修改签收时间
	public void Tc_OnLogisticsNodeChange_001() {
		Logger.start(true, "拼邮订单的运单国际段签收，订单签收状态为false");
		try {

			AgentProductBean productBean = DGSQBuildProxyChainCallService
					.createProxychain((int) cdfMembergoodsOwnerId, 0, false,
							false);
			
			long integral = 10L;

			TradingServiceResp placeOrderResp = SqTradingCallService
					.placeOrderWithCDFMember(productBean, cdfMembergoodsOwnerId, integral);
			
//			TradingServiceResp placeOrderResp = TradingForDGSQCallService
//					.placeOrderWithUserId(productBean, 500967917L);

			Order order = placeOrderResp.getOrderList().get(0);
			
			Long userId = placeOrderResp.getUserId();
			long orderId = order.getOrderId();

			BigDecimal amount = placeOrderResp.getPayableAmount();

			// 支付订单
			Long thirdTradingId = SqTradingCallService.payOrder(userId,
					placeOrderResp.getTradingId());

			// 支付网关支付回调交易
			SQPayGateWayCallService.DGSQNotifyCall(
					String.valueOf(thirdTradingId),
					amount.subtract(BigDecimal.ZERO).toString(),
					userId.intValue(), BigDecimal.ZERO.toPlainString());

			Thread.sleep(5000);

			SqTradingCallService.sellerAcceptOrder(cdfMembergoodsOwnerId, orderId);
			TradingSyncService.getSyncResult(orderId);

			String billCode = SqTradingCallService.delivery(cdfMembergoodsOwnerId, orderId, null, null);
			
//			long orderId = 14091970783L;
//			String billCode = "test007";

			// 国际段签收
			SqTradingCallService.saveLogisticsInfo(billCode);
			
			Thread.sleep(30000);

			OnLogisticsNodeChangeBizParamVo bizParam = new OnLogisticsNodeChangeBizParamVo();

			bizParam.setOrderId(orderId);
			bizParam.setBillCode(billCode);
			bizParam.setNodeStatus(5);
			bizParam.setNodeTime("2021-07-01 00:00:00");

			onLogisticsNodeChangeReq.setAppId("autotest");
			onLogisticsNodeChangeReq.setBizkey(String.valueOf(orderId));
			onLogisticsNodeChangeReq.setBizParam(bizParam);
			onLogisticsNodeChangeReq.setBizType("LogisticsNotify");

			onLogisticsNodeChangeCall.setData(onLogisticsNodeChangeReq);
			onLogisticsNodeChangeCall.callService();

			Logger.verifyEquals("ok", onLogisticsNodeChangeCall.getReturnData(),
					"验证Code");

			// 数据库验证
			OrderPo actualOrder1 = orderShardingWapper
					.selectOrderbyOrderId(orderId);

			Logger.verifyEquals(false, actualOrder1.getLogisticSign(),
					"验证数据库Logistic");

		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	public static void main(String[] args) {

		SqTradingCallService.saveLogisticsInfo("BSQFJB1520673458");
	}
}